#!/usr/bin/env python
# encoding: utf-8
# 求两个集合的差集，集合元素使用文件存储，每行代表一个元素
# 集合A-集合B得到的集合，其元素在A集合中，但不在B集合中
#
# 使用方法：
#         python set-sub.py filel.txt file2.txt
# 注意事项：
#         文件不能有重复元素，可以使用以下unix命令来删除一个文件中的重复行
#         sort file.txt | uniq > file_new.txt

import sys

if len(sys.argv) != 3:
    print "python set_sub.py file1.txt file2.txt"
    sys.exit(-1)

set1 = [line.strip() for line in open(sys.argv[1])]
set2 = [line.strip() for line in open(sys.argv[2])]

set1.sort()
set2.sort()

# At this moment, set1 and set2 are all ordered,
# e.g. set1 = [1 2 3]
#      set2 = [3 4 5]
# then set - set2 = [1 2]
i, j = 0, 0
while i<len(set1) and j<len(set2):
    if set1[i] == set2[j]:
        i = i+1
        j = j+1
    elif set1[i] < set2[j]:
        print set1[i]
        i = i + 1
    else:
        j = j + 1

# Print the remaind elements in set1
for e in set1[i:]:
    print e
    
